package com.example.word_book.service;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.word_book.R;
import com.example.word_book.bean.Word;
import com.example.word_book.utils.SqlHelp;

import java.util.ArrayList;

public class AllWordActivity extends AppCompatActivity {
    private ArrayList<Word> wordArrayList;
    private SqlHelp sqlHelp = new SqlHelp(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all_word);


        init();
    }

    private void init() {
        ListView lv = findViewById(R.id.lv_word);
        wordArrayList = sqlHelp.selectAllWordBySelection(null);
        EditText edt_search = findViewById(R.id.edt_search);
        ImageView img_search = findViewById(R.id.img_search);
        TextView tv_prompt = findViewById(R.id.tv_prompt);

        ImageView img_back = findViewById(R.id.img_back);
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        lv.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return wordArrayList.size();
            }

            @Override
            public Object getItem(int i) {
                return wordArrayList.get(i);
            }

            @Override
            public long getItemId(int i) {
                return i;
            }

            @Override
            public View getView(int i, View view, ViewGroup viewGroup) {
                view = View.inflate(AllWordActivity.this, R.layout.list_item_word, null);
                TextView word = view.findViewById(R.id.tv_word);
                TextView heat = view.findViewById(R.id.tv_heat);
                word.setText(wordArrayList.get(i).getWord());
                heat.setText(String.valueOf(wordArrayList.get(i).getHeat()));
                return view;
            }
        });

        img_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (edt_search.getText().toString().equals(""))
                    Toast.makeText(AllWordActivity.this, "请输入要查询的匹配项", Toast.LENGTH_SHORT).show();
                else {
                    wordArrayList = sqlHelp.selectAllWordBySelection(edt_search.getText().toString());
                    if (wordArrayList.size() == 0)
                        tv_prompt.setVisibility(View.VISIBLE);
                    else
                        tv_prompt.setVisibility(View.GONE);
//                        Toast.makeText(AllWordActivity.this, "未找到匹配项", Toast.LENGTH_SHORT).show();
                    lv.setAdapter(new BaseAdapter() {
                        @Override
                        public int getCount() {
                            return wordArrayList.size();
                        }

                        @Override
                        public Object getItem(int i) {
                            return wordArrayList.get(i);
                        }

                        @Override
                        public long getItemId(int i) {
                            return i;
                        }

                        @Override
                        public View getView(int i, View view, ViewGroup viewGroup) {
                            view = View.inflate(AllWordActivity.this, R.layout.list_item_word, null);
                            TextView word = view.findViewById(R.id.tv_word);
                            TextView heat = view.findViewById(R.id.tv_heat);
                            word.setText(wordArrayList.get(i).getWord());
                            heat.setText(String.valueOf(wordArrayList.get(i).getHeat()));
                            return view;
                        }
                    });
                }
            }
        });
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(AllWordActivity.this,BrowseWordActivity.class);
                intent.putExtra("word",wordArrayList.get(i).getWord());
                startActivity(intent);
            }
        });

    }
}